<?php
class InfoAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('1', '2'));
    }
    
    public function index() {
        $Article = M('Article');
        import("ORG.Util.Page");
        
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $count = $Article->where(array('cat_id' => $id))->count();
            $Page = new Page($count, 25);
            $article = $Article->where(array('cat_id' => $id))->order(array('list_order' => 'asc', 'add_time' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else {
            $count = $Article->count();
            $Page = new Page($count, 25);
            $article = $Article->order(array('add_time' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        
        $this->assign('cat_id', $id);
        $this->assign('article', $article);
        
        $show = $Page->show();
        $this->assign('page',$show);
        
        $this->display();
    }
    
    public function add() {
        $cat_option = '';
        $Category = M('Category');
        // $cat_parent = $Category->where(array('parent_id' => 0))->order(array('list_order' => 'asc'))->select();
        // foreach($cat_parent as $parent_value) {
            // $cat_option .= '<optgroup label="' . $parent_value['cat_name'] . '">';
            // $cat_child = $Category->where(array('parent_id' => $parent_value['cat_id']))->order(array('list_order' => 'asc'))->select();
            // foreach($cat_child as $child_value) {
                // $cat_option .= '<option value="' . $child_value['cat_id'] . '">' . $child_value['cat_name'] . '</option>';
            // }
            // $cat_option .= '</optgroup>';
        // }
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $category = $Category->where(array('cat_id' => $id))->find();
            $this->assign('category', $category);
        } else {
            $id = 0;
            $category = $Category->order(array('list_order' => 'asc'))->select();
            foreach ($category as $cat_value) {
                $cat_option .= '<option value="' . $cat_value['cat_id'] . '">' . $cat_value['cat_name'] . '</option>';
            }
            $this->assign('cat_option', $cat_option);
        }

        $this->assign('cat_id', $id);
        $this->display();
    }
    
    public function insert() {
        if (isset($_POST['submitted'])) {
            $Article = D('Article');
            if ($art = $Article->create()) {
                if ($Article->add()) {
                    $this->success('成功添加文章！');
                } else {
                    $this->error('添加文章失败！');
                }
            } else {
                $this->error($Article->getError());
            }
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Article = M('Article');
            $article = $Article->find($id);
            
            $this->assign('article', $article);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function update() {
        if (isset($_POST['submitted'])) {
            $Article = D('Article');
            
            if ($Article->create()) {
                if (false !== $Article->save()) {
                    $this->success('成功编辑文章！');
                } else {
                    $this->error('编辑文章失败！');
                }
            } else {
                $this->error($Article->getError());
            }
        }
    }
    
    /* 更新排序 */
    public function updateListOrder() {
        if (isset($_POST['submitted'])) {
            $Article = M('Article');
            $list_order = $_POST['list_order'];
            foreach ($list_order as $key => $value) {
                $Article->article_id = $key;
                $Article->list_order = $value;
                $Article->save();
            }
            $this->success('成功更新排序！');
        } else {
            $this->error('非法操作！');
        }
    }
    
    public function delete() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Article = M('Article');
            
            if ($Article->delete($id)) {
                $this->success('成功删除文章！');
            } else {
                $this->error('删除文章失败！');
            }
        }
    }
    
}
?>